//
// Created by lxinp on 2023/5/16.
//

#include "rbtree.h"


template<typename K, typename V>
RBTree<K, V>::RBTree() : root(nullptr) {}


template<typename K, typename V>
RBTree<K, V>::RBTree(const RBTree& other) : root(copy(other.root)) {}


template<typename K, typename V>
RBTree<K, V>::~RBTree() {
    clear(root);
    root = nullptr;
}


template<typename K, typename V>
RBTree<K, V>& RBTree<K, V>::operator=(const RBTree<K, V>& other) {
    if (this != &other) {
        if (root) clear(root);
        root = copy(other.root);
    }
    return *this;
}


template<typename K, typename V>
void RBTree<K, V>::clear() {
    clear(root);
}


template<typename K, typename V>
void RBTree<K, V>::insert(const K& key, const V& value) {
    insert(root, key, value);
}


template<typename K, typename V>
void RBTree<K, V>::remove(const K& key) {
    remove(root, key);
}


template<typename K, typename V>
V RBTree<K, V>::find(const K& key) const {
    return find(root, key);
}


template<typename K, typename V>
typename RBTree<K, V>::RBNode* RBTree<K, V>::copy(const RBTree::RBNode* other) {
    if (!other) return nullptr;
    RBNode* node = new RBNode(other->key, other->value, other->color);
    node->left = copy(other->left);
    node->right = copy(other->right);
    node->left->parent = node;
    node->right->parent = node;
    return node;
}
